/**
 * Created by bianbian3 on 2018/8/24.
 */
import types  from './actionTypes';
import { makeRequestAction }  from './requestAction';
import { serverUrl }  from '../config/config';
import CRUD from '../config/entityOperation';
import Entities from '../constants/entities';
import { setIsFetching } from '../action/fetch';
import { REQUEST_TAIL, FAILURE_TAIL, SUCCESS_TAIL } from '../utils/actionType';
import { get, post } from '../action/request';
import {
	AsyncStorage
} from 'react-native';

function save(assessToken){


	// JSON.stringify(object): JSON对象转换为字符串 用来存储
	AsyncStorage.setItem('assessToken',assessToken,(error)=>{
		if (error) {
		} else  {
		}
	});
}


//注册
export function register(authCode, password, mobile) {

	return dispatch => {
		return new Promise(function(resolve, reject) {
			let formData = new FormData();
			//暂时写死accsetoken
			formData.append("authCode",authCode);
			formData.append("password",password);
			formData.append("mobile",mobile);
				dispatch(post('POST',`http://192.168.2.4:8080/yaguanapi/api/v1/auth/user/register`,'register',formData)).then((res) => {
					resolve(res)
				}).catch((error) => {
					reject(error)

				})


		})
	}



}

export function registerAuthCode(mobile) {

	//authCode && formData.append("authCode",authCode);


	return dispatch => {
		return new Promise(function(resolve, reject) {
			let formData = new FormData();

			//暂时写死accsetoken
			mobile &&  formData.append("mobile",mobile);

			dispatch(post('POST',`http://192.168.2.4:8080/yaguanapi/api/v1/auth/user/get_registe_auth_code`,'registerAuthCode',formData)).then((res) => {
				resolve(res)

			})
		})
	}}

//


//注册
export function forgetPassword(authCode, password, mobile) {

	return dispatch => {
		return new Promise(function(resolve, reject) {
			let formData = new FormData();
			//暂时写死accsetoken
			formData.append("authCode",authCode);
			formData.append("password",password);
			formData.append("mobile",mobile);
			dispatch(post('POST',`http://192.168.2.4:8080/yaguanapi/api/v1/auth/user/modify_user_info`,'modify',formData)).then((res) => {
				resolve(res)
			}).catch((error) => {
				reject(error)

			})
		})
	}
}

export function passwordAuthCode(mobile) {
	//authCode && formData.append("authCode",authCode);
	return dispatch => {
		return new Promise(function(resolve, reject) {
			let formData = new FormData();
			//暂时写死accsetoken
			mobile &&  formData.append("mobile",mobile);
			console.warn(mobile)
			dispatch(post('POST',`http://192.168.2.4:8080/yaguanapi/api/v1/auth/user/get_modify_user_code`,'registerAuthCode',formData)).then((res) => {
				resolve(res)

			})
		})
	}}

//




//
export function login(mobile, password, authCode) {

	//authCode && formData.append("authCode",authCode);


	return dispatch => {
		return new Promise(function(resolve, reject) {
			let formData = new FormData();

			//暂时写死accsetoken
			mobile &&  formData.append("mobile",mobile);
			password && formData.append("password",password);
			dispatch(post('POST',`http://192.168.2.4:8080/yaguanapi/api/v1/auth/user/login`,'login',formData)).then((res) => {
				save(res.object);
				dispatch(receiverUseAccseToken(res.object));
				resolve(res)

			})
		})
	}}

function receiverUseAccseToken(assessToken) {
	let assessTokenObJ = {assessToken};

	const entity = {
		name: Entities.token,
		operation: CRUD.CREATE,
		data: assessTokenObJ,
	};
	const action = makeAction(entity);
	return action;
}

function makeAction(entity) {
	if (entity) {
		const {
			name,
			operation,
			data,
		} = entity;
		// const  isLoading = true;
		return makeRequestAction(name, operation, data);
	}
}


